<template>
  <div >
    <el-form  label-width="80px" style="text-align: left;">
      <el-form-item label="病人姓名">
        <el-input v-model="param.caseName" :readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="开药医生">
        <el-select placeholder="请选择" v-model="param.workerId">
        <el-option :label="doctor.wkName" :key="doctor.id" :value="doctor.id" v-for="doctor in allDoctor"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="药品名称" >
        <el-select placeholder="请选择" v-model="param.mdDrugStoreId">
          <el-option :label="medicine.dsName" :key="medicine.dsNo" :value="medicine.dsNo" v-for="medicine in allMedicine"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开药数量">
        <el-input v-model="param.mdNum"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addMedicineBtn">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="mdName"
        label="药品名称"
        >
      </el-table-column>
      <el-table-column
        prop="mdNum"
        label="数量"
        >
      </el-table-column>
      <el-table-column
        prop="mdBuyPrice"
        label="药品售价/盒">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="delMedicineBtn(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-button type="primary" @click="saveAddMedicineBtn">确定</el-button>
  </div>
</template>

<script>
export default {
  props:{
        message: {
          type: Object,
          required: true
        }
      },

  data() {
    return {
      tableData: [],
      param:{},
      allDoctor:[],
      allMedicine:[],
      sfgNum:'',
    }
  },

  methods: {
    queryAllDoctor(){
      this.$axios.get('api/doctor/queryAllDoctor').then(resp=>{
        if(resp.data.code==200){
          this.allDoctor=resp.data.data
        }
      })
    },

    queryAllMedicine(){
      this.$axios.get('api/doctor/queryAllMedicine').then(resp=>{
        if(resp.data.code==200){
          this.allMedicine=resp.data.data
          // console.log(resp.data)
        }
      })
    },

    addMedicineBtn(){
      this.param.mdPrescriptionId=this.sfgNum
      this.$axios.post('api/doctor/addMedicineDetail',this.param).then(resp=>{
        console.log(resp.data)
        if(resp.data.code==200){
          this.tableData=resp.data.data
          this.$message({
                showClose: true,
                message: '添加药成功',
                type: 'success'
         });
         this.param.mdDrugStoreId=''
         this.param.mdNum=''
        }
      })
    },



    delMedicineBtn(id){
      this.param.id=id
      this.$axios.post('api/doctor/delMedicine',this.param).then(resp=>{
        if(resp.data.code==200){
          this.tableData=resp.data.data
          this.$message({
                showClose: true,
                message: '删除成功',
                type: 'success'
         });
        }
      })
    },

    saveAddMedicineBtn(){
      this.$axios.post('api/doctor/saveAddMedicine',this.param).then(resp=>{
        if(resp.data.code==200){
          this.$message({
                showClose: true,
                message: '已成功开药',
                type: 'success'
         });
         this.tableData=''
         this.createSfg()
        }
      }) 
    },

    createSfg(){
      this.$axios.get('api/doctor/createSfg').then(resp=>{
        this.sfgNum=resp.data.data
      })
    },

    text(){
      this.param.caseId=this.message.caseId
      this.param.caseName=this.message.caseName
     }
  },

  created(){
    this.queryAllDoctor()
    this.queryAllMedicine()
    this.createSfg()
    this.text()
  }
}
</script>

<style scoped>
</style>